using System.Data;
using System.Xml.Linq;
using 用户管理系统.controller;
using 用户管理系统.page;

//以下只是veiw 显示层代码
namespace 用户管理系统 {
    //partial 部分
    //sealed 密封的 不能被继承
    public partial class FMain : Form {
        //哪种性别被选中
        private Gender checkedGender;
        //被选中的行业值为true,数组的索引可以直接强转为枚举Industry
        private readonly bool[] checkedIndustrys = new bool[4];
        public FMain() {
            InitializeComponent();
            //性别单选框被选中事件
            foreach (var item in groupBox_Gender.Controls) {
                if (item is RadioButton radio)
                    radio.CheckedChanged += RadioButton_CheckedChanged_Gender;
            }
            //行业多选框被选中事件
            foreach (var item in groupBox_Industry.Controls) {
                if (item is CheckBox box)
                    box.CheckedChanged += CheckBox_CheckedChanged_Industry;
            }

        }



        private void Btn_Add_Click(object sender, EventArgs e) {
            var name = txt_Name.Text;
            var no = txt_No.Text;
            var gerder = checkedGender;
            var industries = GetIndustries();
            //参数空校验
            if (!Check.CheckEmpty(label_Name.Text, name))
                return;
            if (!Check.CheckEmpty(label_No.Text, txt_No.Text))
                return;

            if (!Check.CheckEmpty(gerder)) {
                DialogResult dialogResult = MessageBox.Show($"{groupBox_Gender.Text}空,是否新增?", "提示"
                    , MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No) {
                    return;
                }
            }
            if (!Check.CheckEmpty(industries)) {
                DialogResult dialogResult = MessageBox.Show($"{groupBox_Industry.Text}空,是否新增?", "提示"
                    , MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No) {
                    return;
                }
            }


            R<User> result = Controller.AddUser(no.Trim(), name.Trim(), gerder, industries);
            if (result.IsFail()) {
                Tip.Error(result.Msg);
            } else {
                LoadList();
                Tip.Tips(result.Msg);
            }
        }
        private void Btn_Delete_Click(object sender, EventArgs e) {
            var no = txt_No.Text;
            //参数空校验
            if (!Check.CheckEmpty(label_No.Text, txt_No.Text))
                return;
            var result = Controller.Delete(no);
            if (result.IsFail()) {
                Tip.Error(result.Msg);
            } else {
                LoadList();
                Tip.Tips(result.Msg);
            }
        }
        private Industry[] GetIndustries() => [.. Enumerable.Range(0, checkedIndustrys.Length)
                             .Where(i => checkedIndustrys[i])
                             .Select(i => (Industry)i)];



        private void RadioButton_CheckedChanged_Gender(object? sender, EventArgs e) {

#pragma warning disable CS8602 // 解引用可能出现空引用。
            checkedGender = (sender as RadioButton).Text switch {
                "男" => Gender.男,
                "女" => Gender.女,
                _ => Gender.未知
            };
#pragma warning restore CS8602 // 解引用可能出现空引用。
        }

        private void CheckBox_CheckedChanged_Industry(object? sender, EventArgs e) {
            var box = sender as CheckBox;
#pragma warning disable CS8602 // 解引用可能出现空引用。
            var index = box.Text switch {
                "政府" => Industry.政府,
                "企业" => Industry.企业,
                "教育" => Industry.教育,
                "其他" => Industry.其他,
                _ => Industry.未知,
            };
#pragma warning restore CS8602 // 解引用可能出现空引用。

            checkedIndustrys[(int)index] = box.Checked;


        }
        private void Form1_Load(object sender, EventArgs e) {

            LoadList();
        }
        private void LoadList() {
            string txt = "账号\t姓名\t性别\t行业\r\n";

            var list = Global.GetUsers()
                .Select(u => $"{u.No}\t{u.Name}\t{u.Gender}\t{string.Join(",", u.Industrys)}")
                .ToList();

            Txt_UserList.Text = txt + string.Join("\r\n", list);
        }
        private void FMain_FormClosed(object sender, FormClosedEventArgs e) {
            Owner?.Show();
        }

       
    }





}
